//fmod.cpp
//Contains the FMOD Implementation
//Justin Renga/Griffin Milsap

// Include Files
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stack>
#include <iostream>
#include "fmod.h"
#include "fmod/fmod.hpp"
#include "fmod/fmod_errors.h"

// Use Standard Namespace
using namespace std;

fmodEX::fmodEX()
{
	system = NULL;
	sound1 = NULL;
	channel = NULL;
	sample = 0;
}

fmodEX::~fmodEX()
{
	FMOD_RESULT result;
	result = sound1->release(); // release / close sound1
	ERRCHECK(result); // check if everything is okay
	result = system->close(); // close the system object
	ERRCHECK(result); // check if everything is okay
	result = system->release(); // release the system object
	ERRCHECK(result); // check if everything is okay
}


void fmodEX::ERRCHECK(FMOD_RESULT result)
{
    if (result != FMOD_OK)
    {
        printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
        exit(-1);
    }
}

void fmodEX::fmod_Init()
{
	channel = 0;
	FMOD_RESULT result;
	result = FMOD::System_Create(&system); // create the system
	ERRCHECK(result);
	result = system->init(32,FMOD_INIT_NORMAL,0); // initilaize FMOD with 32 channels
	ERRCHECK(result);
	result = system->createStream("AroundTheWorld.mp3", FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL, NULL, &sound1);
	ERRCHECK(result);
}
void fmodEX::fmod_refresh()
{
	system->update(); // update the system -- VERY IMPORTANT!
	sample++;
}

void fmodEX::go()
{
	FMOD_RESULT result;
	result = system->playSound(FMOD_CHANNEL_FREE,sound1,false,&channel);
	ERRCHECK(result);
}